import { join } from "path";
import { Common } from "./Common";
import { copyFile, existsSync, readdirSync, readFileSync, statSync, writeFileSync } from "fs";

export class ApplyWeb extends Common {
    private encriptSign  = "";

    private encriptKey = "";

    private buildFloderPath = "";

    constructor( encriptSign: string, encriptKey: string, buildFloderPath: string) {
        super();
        this.encriptSign = encriptSign;
        this.encriptKey = encriptKey;
        this.buildFloderPath = buildFloderPath;
        this.copyHelper();
        this.copyHtml();
    }

    private copyHelper() {
        let fromPath = Editor.url("packages://build-encrypt/process/web_downloader.js", "utf-8");
        let toPath = join(this.buildFloderPath, "assets", "web_downloader.js");
        copyFile(fromPath, toPath, (err) => {
            if (err) {
                Editor.error("【资源替换】", "复制 web_downloader.js 失败", err);
            }
        });
    }

    private searchDir(dirName: string, callback: any) {
        if (!existsSync(dirName)) {
            this.log(`目录不存在 : ${dirName}`);
            return;
        }
        let files = readdirSync(dirName);
        files.forEach(file => {
            let filePath = join(dirName, file.toString());
            let stat = statSync(filePath);
            if (stat.isDirectory()) {
                this.searchDir(filePath, callback); // 递归处理子目录
            } else {
                callback(filePath);
            }
        });
    }

    private copyHtml() {
        let mainJsName = "main.js";
        let settingJsName = "setting.js";
        let physicsJsName = "physics-min.js";
        let cocos2dJSName = "cocos2d-js-min.js";
        let styleDesktopName = "style-desktop.css";
        let styleMobileName = "style-mobile.css";
        let splashName = "splash.png";
        let iconName = "favicon.ico";
        this.searchDir(this.buildFloderPath, (filePath: string) => {
            if (/main\.([0-9 a-z]|\.)*js/.test(filePath)) {
                mainJsName = filePath;
            } else if (/setting\.([0-9 a-z]|\.)*js/.test(filePath)) {
                settingJsName = filePath;
            } else if (/physics-(min)?\.([0-9 a-z]|\.)*js/.test(filePath)) {
                physicsJsName = filePath;
            } else if (/cocos2d-js-(min)?\.([0-9 a-z]|\.)*js/.test(filePath)) {
                cocos2dJSName = filePath;
            } else if (/style-desktop\.([0-9 a-z]|\.)*css/.test(filePath)) {
                styleDesktopName = filePath;
            } else if (/style-mobile\.([0-9 a-z]|\.)*css/.test(filePath)) {
                styleMobileName = filePath;
            } else if (/splash\.([0-9 a-z]|\.)*png/.test(filePath)) {
                splashName = filePath;
            } else if (/favicon\.([0-9 a-z]|\.)*ico/.test(filePath)) {
                iconName = filePath;
            }
        });

        let fromPath = Editor.url("packages://build-encrypt/process/template_web_index.html", "utf-8");
        let toPath = join(this.buildFloderPath, "index.html");

        let htmlContent = readFileSync(fromPath, 'utf-8');
        htmlContent = htmlContent.replace(
            'hyz.register_decripted_downloader(tmp_encriptSign,tmp_encriptKey);',
            `hyz.register_decripted_downloader('${this.encriptSign}','${this.encriptKey}');`);
        htmlContent = htmlContent.replace("main.js", mainJsName);
        htmlContent = htmlContent.replace("setting.js", settingJsName);
        if (physicsJsName.includes("min")) {
            htmlContent = htmlContent.replace("physics-min.js", physicsJsName);
        } else {
            htmlContent = htmlContent.replace("physics.js", physicsJsName);
        }

        if (cocos2dJSName.includes("min")) {
            htmlContent = htmlContent.replace("cocos2d-js-min.js", cocos2dJSName);
        } else {
            htmlContent = htmlContent.replace("cocos2d-js.js", cocos2dJSName);
        }

        htmlContent = htmlContent.replace("style-desktop.css", styleDesktopName);
        htmlContent = htmlContent.replace("style-mobile.css", styleMobileName);
        htmlContent = htmlContent.replace("splash.png", splashName);
        htmlContent = htmlContent.replace("favicon.ico", iconName);

        writeFileSync(toPath, htmlContent, 'utf-8');
    }


}